package com.controller;

import com.dao.PetDao;
import com.po.Pet;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;


@Controller
public class PetController {
/*
    添加宠物
    现只能上传图片路径 未完成向服务器上传图片的功能 by WangWengang 2016.06.05
    */
    @RequestMapping(value = "/doAddPet")
    public String doAdd(Pet pet,Model model)throws Exception{
        //如果未上传图片，选择默认图片
        if(pet.getPhoto()==""){
            String path = "/photo/default.jpg";
            pet.setPhoto(path);
            System.out.println(path);
        }else{
        String path = "/photo/"+pet.getPhoto();
        pet.setPhoto(path);
        System.out.println(path); }

       try {
           PetDao U = new PetDao();
           U.addPet(pet);
           model.addAttribute("msg","添加宠物成功");
           return "/petSearch.jsp";
       } catch (Exception e){
           model.addAttribute("msg",e.getMessage());
           return "/petSearch.jsp";
       }
    }
/*
    查询宠物及宠物主人
    */

    @RequestMapping(value = "/doFindPet")
    public String doFindPet(Pet pet,Model model)throws Exception{
        try {
            PetDao U = new PetDao();
            List<Pet> pets=U.findPet(pet);
            model.addAttribute("cs",pets);
            return "/petSearchResult.jsp";
        } catch (Exception e){
            model.addAttribute("msg",e.getMessage());
            return "/petSearch.jsp";
        }
    }
/*
    删除宠物
    */
    @RequestMapping(value = "/doDelPet")
    public String doDelPet(Pet id,Model model)throws Exception {
        PetDao Delete = new PetDao();
        Delete.deletePet(id);
        model.addAttribute("msg","删除宠物成功");
        return "/petSearch.jsp";
    }
}
